﻿using PLCS.Application.Contracts.PLCConfigApp.Dtos;
using PLCS.Application.Contracts.PLCReadDataApp.Dtos;
using PLCS.Client.IService.ConfigManager;
using PLCS.Domain.PLCConfigs;

using System.Collections.ObjectModel;

using Volo.Abp.Application.Dtos;

namespace PLCS.Client.Service.ConfigManager;

public class PLCConfigService : BaseService<PLCConfigDto, PLCConfigEditDto>, IPLCConfigService
{
    public async Task<PLCConfigDto> GetByNoAsync(string pLCNo)
    {
        RequestParam requestParam = new(method: Method.Get, route: $"pLCConfig/byPLCNo?pLCNo={pLCNo}");
        var apiRes = await restClient.ExecuteAsync<PLCConfigDto>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return null;
    }

    public override async Task<IEnumerable<PLCConfigDto>> GetListAsync()
    {
        RequestParam requestParam = new(method: Method.Get, route: $"pLCConfig/plcConfigList");
        var apiRes = await restClient.ExecuteAsync<ObservableCollection<PLCConfigDto>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return Enumerable.Empty<PLCConfigDto>();
    }

    public override async Task DeleteByIdAsync(Guid Id)
    {
        RequestParam requestParam = new(method: Method.Delete, route: $"pLCConfig/{Id}");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task<PagedResultDto<PLCConfigDto>> GetListBySearchAsync(int skipCount, int maxResultCount, string? plcNo, string? ipAddress)
    {
        var route = $"pLCConfig/bySearch?SkipCount={skipCount}&MaxResultCount={maxResultCount}";
        if (!string.IsNullOrEmpty(plcNo))
            route += $"&plcNo={plcNo}";
        if (!string.IsNullOrEmpty(ipAddress))
            route += $"&ipAddress={ipAddress}";

        RequestParam requestParam = new(method: Method.Get, route: route);
        var apiRes = await restClient.ExecuteAsync<PagedResultDto<PLCConfigDto>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return new PagedResultDto<PLCConfigDto>();
    }
}